package com.eddie.community.controller;

import com.eddie.community.entity.DiscussPost;
import com.eddie.community.entity.Page;
import com.eddie.community.entity.User;
import com.eddie.community.service.DiscussPostService;
import com.eddie.community.service.LikeService;
import com.eddie.community.service.MessageService;
import com.eddie.community.service.impl.UserServiceImpl;
import com.eddie.community.util.CommunityConstant;
import com.eddie.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController {

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserServiceImpl userServiceImpl;

    @Autowired
    private MessageService messageService;
    @Autowired
    private LikeService likeService;
    @Autowired
    private HostHolder hostHolder;

    @RequestMapping(path = "/index", method = RequestMethod.GET)
    public String getIndexPage(Model model, Page page , @RequestParam(name = "orderMode", defaultValue = "0") int orderMode) {
        // 方法调用前,SpringMVC会自动实例化Model和Page,并将Page注入Model.
        // 所以,在thymeleaf中可以直接访问Page对象中的数据.
        page.setRows(discussPostService.findDiscussPostRows(0));
        page.setPath("/index?orderMode="+orderMode);
        List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit(),orderMode);
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (list != null) {
            for (DiscussPost post : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("post", post);
                User user = userServiceImpl.findUserById(post.getUserId());
                map.put("user", user);
                // 点赞数量
                long likeCount = likeService.findEntityLikeCount(CommunityConstant.ENTITY_TYPE_POST, post.getId());
               map.put("likeCount",likeCount);
                discussPosts.add(map);
            }
        }

/*        //使用拦截器替代
User currentUser = hostHolder.getUser();
        if (currentUser!= null) {
            // 查询未读消息数量
            int letterUnreadCount = messageService.findLetterUnreadCount(currentUser.getId(), null);
            model.addAttribute("letterUnreadCount", letterUnreadCount);
            int noticeUnreadCount = messageService.findNoticeUnreadCount(currentUser.getId(), null);
            model.addAttribute("noticeUnreadCount", noticeUnreadCount);

        }*/
        model.addAttribute("orderMode", orderMode);
        model.addAttribute("discussPosts", discussPosts);
        return "/index";
    }

    @RequestMapping(path = "/error", method = RequestMethod.GET)
    public String getErrorPage() {
       return "/error/500";
    }

    @RequestMapping(path = "/denied", method = RequestMethod.GET)
    public String getDniedPage() {
        return "/error/denied";
    }
}
